﻿using System;

namespace _1021_remove_outermost_parentheses
{
    class Program
    {
        static void Main (string[] args)
        {

            Console.WriteLine (RemoveOuterParentheses("(()())(())"));
        }

        public static string RemoveOuterParentheses (string S)
        {
            char[] str = new char[S.Length]; // 用string 会有过多的解包封装 内存消耗过多
            int count = 0;
            int num = 0;
            for (int i = 0;i < S.Length;i++)
            {
                if (S[i] == '(') count++;
                if(count > 1)
                {
                    str[num++] = S[i];
                    if (S[i] == ')') count--;
                }
                else if(S[i] == ')')
                {
                    count--;
                }
            }

            return new string(str, 0,num);
        }
    }
}
